import React from 'react'
import { Button } from 'antd'
import PropTypes from 'prop-types'
import { Editor } from '@tinymce/tinymce-react'
import styles from './index.scss'
// import defaultplugins from './plugins'
import defaultToolbar from './toolbar'
import UploadImage from './UploadImage'

class Tinymce extends React.Component {
  static propTypes = {
    tinymceId: PropTypes.string,
    content: PropTypes.string,
    toolbar: PropTypes.array,
    menubar: PropTypes.string,
    height: PropTypes.number,
    getContent: PropTypes.func,
    onSave: PropTypes.func,
  }

  static defaultProps = {
    tinymceId: `react-tinymce-${+new Date()}${(Math.random() * 1000).toFixed(0)}`,
    menubar: 'file edit insert view format table',
    height: 430,
    toolbar: [],
  }

  constructor(props) {
    super(props)
    this.state = {
      content: '',
      defaultVal: '123',
      options: {},
    }
  }

  // componentDidMount() {
  //   this.initTinymce()
  // }
  initTinymce() {
    const { getContent } = this.props

    const options = {
      language: 'zh_CN',
      height: this.props.height,
      min_height: 400,
      width: '100%',
      resize: true,
      body_class: 'panel-body ',
      object_resizing: false,
      toolbar: defaultToolbar,
      menubar: this.menubar,
      // plugins: defaultplugins,
      end_container_on_empty_block: true,
      powerpaste_word_import: 'clean',
      code_dialog_height: 450,
      code_dialog_width: 1000,
      advlist_bullet_styles: 'square',
      advlist_number_styles: 'default',
      imagetools_cors_hosts: ['www.tinymce.com', 'codepen.io'],
      default_link_target: '_blank',
      link_title: false,
      nonbreaking_force_tab: true, // inserting nonbreaking space &nbsp; need Nonbreaking Space Plugin
      handleEditorChange: (content, editor) => {
        getContent(content)
        console.log('Content was updated:', content)
      },
    }

    this.setState({
      options,
    })
  }

  /**
   * 上传图片成功回调
   */
  imageSuccessCBK(arr) {
    const { tinymceId } = this.props
    arr.forEach((v) => {
      window.tinymce.get(tinymceId).insertContent(`<img class="wscnph" src="${v.url}" >`)
    })
  }

  handleEditorChange(content, editor) {
    this.props.getContent(content)
    this.setState({ content })
    console.log('Content was updated:', content)
  }

  render() {
    const { onSave, content } = this.props

    const options = {
      language: 'zh_CN',
      height: this.props.height,
      width: '100%',
      resize: true,
      body_class: 'panel-body ',
      object_resizing: false,
      toolbar: defaultToolbar,
      menubar: this.menubar,
      // plugins: defaultplugins,
      end_container_on_empty_block: true,
      powerpaste_word_import: 'clean',
      code_dialog_height: 450,
      code_dialog_width: 1000,
      advlist_bullet_styles: 'square',
      advlist_number_styles: 'default',
      imagetools_cors_hosts: ['www.tinymce.com', 'codepen.io'],
      default_link_target: '_blank',
      link_title: false,
      nonbreaking_force_tab: true, // inserting nonbreaking space &nbsp; need Nonbreaking Space Plugin
      handleEditorChange: (content, editor) => {
        this.props.getContent(content)
        console.log('Content was updated:', content)
      },
    }

    return (
      <div className={styles['tinymce-components']}>
        <div className="content">
          <p>
            富文本是管理后台一个常见的功能，经过多方筛选，最后决定使用
            <a href="https://www.tiny.cloud/" target="_blank" rel="noopener noreferrer">
              Tinymce
            </a>
            。
          </p>
        </div>
        <div className="tinymce-container">
          <Editor
            inline={false}
            selector="editorStateRef" // 选择器
            apiKey="i9loif5tptrktatd5nsxz66i1iqnijfpotc1lqs9mf24za7q"
            initialValue={content}
            // init={this.state.options}
            init={options}
            onEditorChange={this.handleEditorChange.bind(this)}
          />
          <div className="editor-custom-btn">
            <UploadImage
              className="editor-upload-btn"
              imageSuccessCBK={(arr) => {
                this.imageSuccessCBK(arr)
              }}
            />
          </div>
          <div className="handle-action">
            <Button
              type="primary"
              onClick={() => {
                onSave(this.state.content)
              }}
            >
              保存
            </Button>
          </div>
        </div>
      </div>
    )
  }
}

export default Tinymce
